package calculate24.play.SelfCalculate;

import static android.app.Activity.RESULT_OK;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

import calculate24.play.R;

abstract class SolutionsDialog {
    public static void create(AppCompatActivity context, ArrayList<String> solutions) {
        Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.self_calculate_finish);
        LinearLayout resultZone = dialog.findViewById(R.id.selfCalculateResultZone);
        TextView prompt = dialog.findViewById(R.id.selfCalculateResultPrompt);
        prompt.setText(context.getString(R.string.solutions_prompt, solutions.size()));
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < solutions.size(); ++i) {
            sb.append(context.getString(R.string.solution, i + 1, solutions.get(i)));
            sb.append('\n');
        }
        TextView textView = new TextView(dialog.getContext());
        textView.setText(sb);
        textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        textView.setTextAppearance(R.style.resultStyle);
        resultZone.addView(textView);
        dialog.findViewById(R.id.selfCalculateExitButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.finish();
            }
        });
        dialog.findViewById(R.id.selfCalculateReplayButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.setResult(RESULT_OK, new Intent().putExtra("replay?", true));
                context.finish();
            }
        });
        dialog.show();
    }
}
